package com.mdms.logger;

import java.util.Properties;

import org.apache.log4j.Logger;


import com.mdms.utils.MDmsLoginContext;

public class SimplyMDLogger implements ISimplyMDLogger {

	private Logger logger;

	private String clazzName;

	public SimplyMDLogger (Class className){
		logger=Logger.getLogger(className);
		clazzName=className.getName();
	}
	public void debug(String methodName, String message) {
		logger.debug(prepareMessage(methodName,message));
	}
	@Override
	public void warrning(String methodName, String message) {
		logger.warn(prepareMessage(methodName,message));

	}

	@Override
	public void error(String methodName, String message) {
		logger.error(prepareMessage(methodName,message));

	}

	@Override
	public void error(String methodName, int errorCode, String errorDesc) {
		logger.error(prepareMessage(methodName,null,String.valueOf(errorCode),errorDesc));

	}

	@Override
	public void error(String methodName, Exception e) {
		logger.error(prepareMessage(methodName,null,null,null),e);

	}

	@Override
	public void fatal(String methodName, int errorCode, String errorDesc) {
		logger.debug(prepareMessage(methodName,null,String.valueOf(errorCode),errorDesc));

	}

	@Override
	public void fatal(String methodName, Exception e) {
		logger.debug(prepareMessage(methodName,null,null,null),e);

	}

	private String prepareMessage(String methodName,String message){
		return prepareMessage(methodName, message, null, null);
	}


	private String prepareMessage(String methodName,String message,String errorCode,String errorDesc){
		Properties props=MDmsLoginContext.getContext();
		StringBuffer buffer = new StringBuffer();
		Properties props1 = MDmsLoginContext.getContext();
		buffer.append(clazzName);
		buffer.append("|");
		if(props!=null){
		buffer.append(props1.getProperty(MDmsLoginContext.USER_ID));
		buffer.append("|");
		buffer.append(props1.getProperty(MDmsLoginContext.SSN_ID));
		buffer.append("|");
		buffer.append(props1.getProperty(MDmsLoginContext.SSN_IP));
		buffer.append("|");
		}
		buffer.append(methodName);
		buffer.append("|");
		buffer.append(errorCode);
		buffer.append("|");
		buffer.append(message);
		buffer.append("|");
		buffer.append(errorDesc);
		return buffer.toString();

	}



}
